/*
 * AbstractFileSystem.java
 *
 * Created on December 3, 2006, 9:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eu.somatik.somaftp.logic;

import eu.somatik.somaftp.SomaFTP;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.DefaultListModel;

/**
 *
 * @author francisdb
 */
public abstract class AbstractFileSystem<T extends FileInterface> implements FileSystemInterface{
    
    private Vector<FileSystemListener> listeners;
    
    private SomaFTP somaFTP;
    
    private Class<T> tClass;
    
    private String name;
    
    private Map<String,String> properties;
    
    public AbstractFileSystem(SomaFTP somaFTP, Class<T> tClass){
        this.somaFTP = somaFTP;
        this.tClass = tClass;
        this.listeners = new Vector<FileSystemListener>();
        this.properties = new HashMap<String, String>();
    }
    
    public String getProperty(String key){
        return properties.get(key);
    }
    
    public void setProperty(String key, String value){
        properties.put(key, value);
    }
    
    public void connect(final DefaultListModel logModel) throws FileSystemException{
    }
    
    public void disconnect() {
    }
    
    public abstract String getPath();
    
    public T cast(Object object){
        return tClass.cast(object);
    }
    
    public void addListener(FileSystemListener listener){
        listeners.add(listener);
    }
    
    protected void fireUpdate() {
        String path = getPath();
        List<FileInterface> list = list(true);

        for(FileSystemListener listener:listeners){
            listener.update(list, path);
        }
    }
    
    public void request(Object object) {
        T requestedFile = cast(object);
        if( requestedFile.isDirectory() || requestedFile.isLink()){
            changeDirectory(requestedFile);
            fireUpdate();
            // TODO handle selection of first or upper!
        }else{
            // open file?
        }
    }   

    public void queue(Object object) {
        T requestedFile = cast(object);
        somaFTP.queue(new FileInterface[]{requestedFile});
    }
    
    public void delete(Object...objects) throws FileSystemException{
        for(Object object:objects){
            delete(object);
        }
        fireUpdate();
    }
    
    protected abstract void delete(Object object) throws FileSystemException;
    
    
    public abstract void changeDirectory(T dir);

    protected abstract void cd(String path);
    
    public final void changeDirectory(String path) {
        if(path != getPath()){
            cd(path);
            System.out.println(this.getClass().getSimpleName()+" new path: " + getPath());
            fireUpdate();
            // TODO handle selection of first or upper or prev!
        }else{
            System.out.println(this.getClass().getSimpleName()+" no path change needed");
        }
    }
    

    
}
